package com.ywk.base.common.office.excel.exportExcel;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zbh
 * @date 2022/7/13 16:34
 */
public class ExportTest {

    /**
     * 常规导出
     * @param request
     * @param response
     * @throws IOException
     */
    public void generalExport(HttpServletRequest request, HttpServletResponse response) throws IOException {
        XSSFWorkbook wb = new XSSFWorkbook();//创建HSSFWorkbook对象

        //excel表字段
        String [] excelCol = {"姓名", "职称/资质", "职类", "岗位", "学历", "专业", "学校", "入职年限",
                "工具类别", "擅长技术方法", "技术成果贡献", "创新性贡献（月度评分）", "创新性贡献（年度评分）","成果热度排行", "项目名称"};

        Sheet sheet=wb.createSheet("员工知识地图列表");//建立sheet对象
        Row row1=sheet.createRow(0); //在sheet里创建第一行，参数为行索引
        for (int i = 0; i < excelCol.length; i++) {
            sheet.setColumnWidth(i,150*35);
            row1.createCell(i).setCellValue(excelCol[i]);
        }
        List<Map<String,Object>> resultMap =new ArrayList<>();//后端获取的数据

        int i =1;
        for (Map<String,Object> map:resultMap ){
            Row row=sheet.createRow(i);
            row.createCell(0).setCellValue(map.get("NAME")==null?"":map.get("NAME").toString());
            row.createCell(1).setCellValue(map.get("ZC")==null?"":map.get("ZC").toString());
            row.createCell(2).setCellValue(map.get("ZL")==null?"":map.get("ZL").toString());
            row.createCell(3).setCellValue(map.get("GW")==null?"":map.get("GW").toString());
            row.createCell(4).setCellValue(map.get("XL")==null?"":map.get("XL").toString());
            row.createCell(5).setCellValue(map.get("ZY")==null?"":map.get("ZY").toString());
            row.createCell(6).setCellValue(map.get("SCHOOL")==null?"":map.get("SCHOOL").toString());
            String yf=map.get("yf")==null?"": map.get("yf").toString();
            String GZNX=map.get("GZNX")==null?"": map.get("GZNX").toString();
            String nn="";
            if (GZNX == "0") {
                nn = yf + "月";
            }
            if (GZNX != "0" && GZNX != "") {
                nn = GZNX + "年" + yf + "月";
            }
            row.createCell(7).setCellValue(nn);
            row.createCell(8).setCellValue(map.get("GJLX")==null?"":map.get("GJLX").toString());
            row.createCell(9).setCellValue(map.get("JSFF")==null?"":map.get("JSFF").toString());
            row.createCell(10).setCellValue(map.get("sccgNum")==null?"":map.get("sccgNum").toString());
            row.createCell(11).setCellValue(map.get("ydCxpf")==null?"":map.get("ydCxpf").toString());
            row.createCell(12).setCellValue(map.get("ndCxpf")==null?"":map.get("ndCxpf").toString());
            row.createCell(13).setCellValue(map.get("rdph")==null?"":map.get("rdph").toString());
            row.createCell(14).setCellValue(map.get("xmid")==null?"":map.get("xmid").toString());
            i++;
        }


        //输出Excel文件
        String fileName =java.net.URLEncoder.encode("员工知识地图列表.xlsx", "UTF-8");
        OutputStream output=response.getOutputStream();
        response.reset();
        //设置响应头，
        response.setHeader("Content-disposition", "attachment; filename="+fileName);
        response.setContentType("application/msexcel");
        wb.write(output);
        output.close();
    }
}
